package com.dong.annotation;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 *    表达式 包含  1.
 *    1. desiginator
 *      1.1 匹配方法
 *          1)execution()
 *      1.2匹配注解
 *          1) @target
 *          2) args()
 *          3) within()
 *          4) annotation
 *        1.3 匹配包/类型
 *          within()
 *        1.4 匹配对象
 *              1). this(0
 *              2). bean()
 *              3). target()
 *          1.5 匹配参数
 *              args()
 *    2. wilcards
 *      2.1 *
 *      2.2 ..
 *      2.3 +   //指定类及子类
 *    3. operators
 *      3.1 &&
 *      3.2  ||
 *      3.3 !
 */
@Aspect
@Component
public class Info {
    @Pointcut("within(com.dong.service.ProductService)") //匹配类
    public void withIn(){    }
    @Pointcut("within(com.dong..*)")//匹配子包
    public void withIn2(){    }

    /****************匹配对象*************************/
    @Pointcut("this(com.dong.service.ProductService)")//支持动态生成的方法
    public void thisDemo(){}
    @Pointcut("target(com.dong.service.ProductService)")
    public void targetDemo(){}
    @Pointcut("bean(*Service)")
    public void beanDemo(){}
    /****************匹配对象end*************************/
    /****************参数匹配*************************/
    @Pointcut("execution(* *..find*(Long))")
    public void argsDemo1(){}
    @Pointcut("args(Long)")
    public void argDemo2(){}
    @Pointcut("execution(* *..find*(Long,..))")
    public void argDemo3(){}
    @Pointcut("args(Long,..)")
    public void argDemo4(){}


    /****************参数匹配end*************************/

    /****************注解匹配*************************/
    @Pointcut("@annotation(com.dong.security.AdminOnly)")
    public void annoDemo(){}
    @Pointcut("@within(com.dong.security.AdminOnly)")
    public void annoWithinDemo(){}
    @Pointcut("@target(com.dong.security.AdminOnly)")
    public void annoTargetDemo(){}
@Pointcut("@args(com.dong.security.AdminOnly)")
    public void annnoArgsDemo(){}

    /****************注解匹配end*************************/
/**
 * execution  可以指定只拦截抛出某种异常的方法  ,  .. 表示至少一个参数
 */


/**************advice******************/
@Before(value="annoWithinDemo()&&within(com.dong)&&args(userId)")
    public void beforeWithArgs(JoinPoint joinPoint,Long userId){

}
@AfterReturning(value = "annoWithinDemo() && within(com.dong)",returning = "returnValue")
    public void getResults(Object returnValue) {
    if(returnValue!=null) {
        System.out.println("after return ,result: "+returnValue);
    }
}


}
